/**
 * @FileName: ProductOrderService.java
 * @Author
 * @Description:
 * @Date 2016年5月23日 下午5:13:13
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.service;

import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.util.InterfaceUtil;

public class VideoService{
    // private String dprUrlPrefix =
    // SystemConfigUtil.getSystemConfig().getCmccHostPrefix();

    public static final VideoService service = new VideoService();

    /**
     * @Title: getSqlSelect
     * @Description:报表每行显示的信息，店员手机号，店员姓名，工号，所属机构读取的快照，其他从memberorg取
     * @return String
     */
    public String getSelect(){
        String sql = " select v.*,(select user_name from sys_user u where v.created_by = u.id) as creator,(select user_name from sys_user u where v.last_updated_by = u.id) as updator ";
        return sql;
    }

    public String getSqlFilter(ConcurrentHashMap<String, Object> searchInfo){
        String sqlExceptSelect = " from video v where 1=1 and v.is_deleted = 0 ";

        for(Entry<String, Object> item : searchInfo.entrySet()){
            sqlExceptSelect += appendQueryFilter(item);
        }
        sqlExceptSelect += " order by v.sort asc,v.last_updated_time DESC";

        return sqlExceptSelect;
    }

    private Object getOrgLimit(String orgId){
        String sql = " and v.org_id in("
                + InterfaceUtil.getChlidOrgIdSql(Integer.parseInt(orgId), Constant.MAX_ORG_LEVEL, true) + ") ";
        return sql;
    }

    /**
     * @Title: buildQuery
     * @Description:追加查询条件
     */
    private String appendQueryFilter(Entry<String, Object> item){
        StringBuilder sb = new StringBuilder();
        Object valueObj = item.getValue();
        String value = "";
        String key = item.getKey();
        if(null != valueObj){
            value = String.valueOf(valueObj);
        }
        switch(key){
            case "name":
                sb.append(getAndLikeFilter("v.name", value));
                break;
            case "enabled":
                sb.append(getAndLikeFilter("v.enabled", value));
                break;
            case "org_id":
                sb.append(getOrgLimit(value));
                break;
            default:
                break;
        }
        return sb.toString();
    }

    /**
     * @Title: getAndLikeFilter
     * @Description:获取KeyName和value模糊查询条件
     */
    private String getAndLikeFilter(String keyName, String value){
        return String.format(" AND %s LIKE '%%%s%%' ", keyName, value);
    }
}
